package com.yuzhitong.shapi.widget;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.bean.UpdateBean;
import com.yuzhitong.shapi.util.DownloadService;

/**
 * Created by Brsya
 * CreateDate: 2022/4/19  16:30
 * Description:
 * 隐私协议弹框
 */
public class UpdateDialog extends CenterDialog implements View.OnClickListener {
    private Context context;
    private View rootView;
    private TextView tvUpdateLogTitle;
    private TextView tvUpdateLogValue;
    private DownloadTextView textConfirm;
    private TextView textCancel;


    private OnClickBtnListener onClickBtnListener;

    private UpdateBean updateBean;

    private boolean downloadSuccess;
    private String downloadPath;

    public UpdateDialog(Context context, UpdateBean updateBean) {
        super(context);
        this.context = context;
        this.updateBean = updateBean;

        setContentView(rootView = LayoutInflater.from(context).inflate(R.layout.dialog_update, null, false));

        initView();
    }

    private void initView() {

        textCancel = (TextView) rootView.findViewById(R.id.text_cancel);
        textConfirm = (DownloadTextView) rootView.findViewById(R.id.text_confirm);

        tvUpdateLogTitle = (TextView) rootView.findViewById(R.id.tv_update_log_title);
        tvUpdateLogValue = (TextView) rootView.findViewById(R.id.tv_update_log_value);

        tvUpdateLogValue.setText(updateBean.getJournal());
        if (!updateBean.isForceUp()) {
            textCancel.setVisibility(View.VISIBLE);
        }

        textCancel.setOnClickListener(this);
        textConfirm.setOnClickListener(this);

        setKeyListener(false);
        alertDialog.setCancelable(false);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.text_cancel) {
            dismiss();
            if (onClickBtnListener != null) {
                onClickBtnListener.onDismiss();
            }
        } else if (id == R.id.text_confirm) {
            if (onClickBtnListener != null) {
                onClickBtnListener.onConfirm(updateBean);
            }
        }
    }

    @Override
    public void dismiss() {
        Intent intent = new Intent(context, DownloadService.class);
        context.stopService(intent);
        super.dismiss();
    }

    public void setOnClickBtnListener(OnClickBtnListener onClickBtnListener) {
        this.onClickBtnListener = onClickBtnListener;
    }

    public void updateProgress(int progress) {
        if (progress >= 100) {
            textConfirm.setClickable(true);
            textConfirm.setDownloadSuccessText("安装");
            return;
        }
        textConfirm.setClickable(false);
        textConfirm.setProgressPosition(progress);
    }


    public void downloadError() {
        textConfirm.setClickable(true);
        textConfirm.setDownloadNoText("下载失败，请重试");
    }

    public interface OnClickBtnListener {
        void onDismiss();

        void onConfirm(UpdateBean updateBean);
    }
}
